package com.cpi.cornpp.common.files;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.common.files.dto.SAPComponentCornFactorDTO;
import com.cpi.cornpp.common.files.dto.SAPComponentDTO;
import com.cpi.cornpp.common.files.exception.SAPComponentFileException;
import com.cs.common.pool.exception.PoolException;
import com.cs.common.utils.NumberUtil;
/**
 * Componente que parsea el archivo Corn Factor
 * @author Edgar Joao
 * @see SAPComponentFile
 */
public class SAPComponentCornFactor extends SAPComponentFile {
	private static final String CHAR_DELIMITER = "\t";//$NON-NLS-1$
	private static final int OFF_SET_INIT = 1;
	private static final int OFF_SET_END = 1;
	private static final String SQL_INSERT_CORN_FACTOR = "INSERT INTO sap_corn_factor ([plant] ,[material] ,[fiscal_year] ,[period] ,[corn_equivalence] ,[product_mt] ,[grind] ,[factor]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";//$NON-NLS-1$
	
	public SAPComponentCornFactor(String argFileName) {
		super(argFileName);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#parse(java.util.List)
	 */
	public List parse(List list) throws SAPComponentFileException {		
		List sapComponentCornFactorDTOs = null;
		Iterator iterator = null;
		String line = null;
		SAPComponentCornFactorDTO sapComponentCornFactorDTO = null;
		StringTokenizer stringTokenizer = null;
		String token = null;
		int totalReg = 0;
		try{
			iterator = list.iterator();
			totalReg = list.size();
			sapComponentCornFactorDTOs = new ArrayList(0);
			for( int mainIndex = 0; mainIndex < totalReg - OFF_SET_END ; mainIndex++ ){			
				line = (String)iterator.next();
				if( mainIndex+1 > OFF_SET_INIT ){
					stringTokenizer = new StringTokenizer(line, CHAR_DELIMITER, false);
					sapComponentCornFactorDTO = new SAPComponentCornFactorDTO();
					for(int index = 0; stringTokenizer.hasMoreTokens(); index++){
						token = stringTokenizer.nextToken();
						switch(index){
							case 0:	sapComponentCornFactorDTO.setPlant(token);
										break;
							case 1:	sapComponentCornFactorDTO.setMaterial(token);
										break;
							case 2: sapComponentCornFactorDTO.setFiscalYear(NumberUtil.parseInt(token));
										break;
							case 3:	sapComponentCornFactorDTO.setPeriod(NumberUtil.parseInt(token));								
										break;									
							case 4:  try{								      
								    	 sapComponentCornFactorDTO.setCornEquivalence(NumberUtil.parseDouble(token.replaceAll("\"", StringUtils.EMPTY)));								      
									 }catch(NumberFormatException nfe){ 
										 //sapComponentCornFactorDTO.setCornEquivalence(-1);
										 System.out.println("NUMERO INVALIDO.");										
									 }
										break;
							case 5:	try{
										sapComponentCornFactorDTO.setProductMT(NumberUtil.parseDouble(token.replaceAll("\"", StringUtils.EMPTY)));
									}catch(NumberFormatException nfe){
										sapComponentCornFactorDTO.setProductMT(-1);
										System.out.println("NUMERO INVALIDO.");
										nfe.printStackTrace();
									}
										break;
							case 6: try{
										sapComponentCornFactorDTO.setGrind(NumberUtil.parseDouble(token.replaceAll("\"", StringUtils.EMPTY)));
									}catch(NumberFormatException nfe){
										sapComponentCornFactorDTO.setGrind(-1);
										System.out.println("NUMERO INVALIDO.");
										nfe.printStackTrace();
									}
										break;
							case 7: try{
										sapComponentCornFactorDTO.setFactor(NumberUtil.parseDouble(token.replaceAll("\"", StringUtils.EMPTY)));
									}catch(NumberFormatException nfe){
										sapComponentCornFactorDTO.setFactor(-1);
										System.out.println("NUMERO INVALIDO.");
										nfe.printStackTrace();
									}
										break;
						}
					}
					sapComponentCornFactorDTOs.add(sapComponentCornFactorDTO);
				}
			}
		}catch(Exception exception){
			System.out.println("MATERIAL: " + sapComponentCornFactorDTO.getMaterial());
			SAPComponentFileException sapComponentFileException = null;
			sapComponentFileException = new SAPComponentFileException(exception, CornPPException.LAYER_SERVICE, CornPPException.ACTION_LISTS);
			throw sapComponentFileException;
		}
		return sapComponentCornFactorDTOs;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insert(com.cpi.cornpp.common.files.dto.SAPComponentDTO)
	 */
	public void insert(SAPComponentDTO sapComponentDTO) throws SAPComponentFileException {		
		SAPComponentCornFactorDTO sapComponentCornFactorDTO = null;
		PreparedStatement preparedStatement = null;
		try{
			sapComponentCornFactorDTO = (SAPComponentCornFactorDTO)sapComponentDTO;
			preparedStatement = prepareSQL(SQL_INSERT_CORN_FACTOR, dsName);						
			preparedStatement.setString(1, sapComponentCornFactorDTO.getPlant());
			preparedStatement.setString(2, sapComponentCornFactorDTO.getMaterial());
			preparedStatement.setInt(3, sapComponentCornFactorDTO.getFiscalYear());
			preparedStatement.setInt(4, sapComponentCornFactorDTO.getPeriod());
			preparedStatement.setDouble(5, sapComponentCornFactorDTO.getCornEquivalence());
			preparedStatement.setDouble(6, sapComponentCornFactorDTO.getProductMT());
			preparedStatement.setDouble(7, sapComponentCornFactorDTO.getGrind());
			preparedStatement.setDouble(8, sapComponentCornFactorDTO.getFactor());
			executeInsert(preparedStatement);
			closeConnection(preparedStatement.getConnection());
		}catch(PoolException poolException){
			SAPComponentFileException sapComponentFileException = null;
			poolException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}catch(SQLException sqlException){
			SAPComponentFileException sapComponentFileException = null;
			sqlException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (sqlException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insertAll(java.util.List)
	 */
	public int insertAll(List sapComponentCornFactorDTOs) throws SAPComponentFileException {
		int totalInserted = 0;
		Iterator iterator = null;
		SAPComponentCornFactorDTO sapComponentCornFactorDTO;
		try{
			iterator = sapComponentCornFactorDTOs.iterator();
			while(iterator.hasNext()){
				sapComponentCornFactorDTO = (SAPComponentCornFactorDTO)iterator.next();
				try{
					insert(sapComponentCornFactorDTO);
					totalInserted++;
				}catch(SAPComponentFileException sapComponentFileException){
					System.out.println("Error DTO (SAPComponentCornFactorDTO) ["+sapComponentFileException.getMessage()+"] : " + sapComponentCornFactorDTO);
				}
			}
		} catch(Exception exception){
			SAPComponentFileException sapComponentFileException = null;
			exception.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (exception, CornPPException.LAYER_DAO,CornPPException.ACTION_SELECT);
			throw sapComponentFileException;
		}
		return totalInserted;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#size()
	 */
	public int size() throws SAPComponentFileException {
		int size = 0;
		List records = null;
		try{
			records = getContent();
			size = records.size() - OFF_SET_INIT - OFF_SET_END;
		}finally{
			records = null;
		}
		return size;
	}
}
